package handler;

import java.io.File;

import java.util.Locale;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import windows.Ferestre;

import data.Angajat;
import data.AngajatiData;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Number;

public class ExportFiles {
	private Ferestre fereastra;

	public ExportFiles(Ferestre ferestre) {

		fereastra = ferestre;

	}

	public void setRaifeissenExportFile(AngajatiData angajati) throws Exception {
		JFileChooser fc = new JFileChooser();
		File file;
		String path = "";

		int i = 0;
		FileNameExtensionFilter filter = new FileNameExtensionFilter(".xls",
				"All files");
		fc.addChoosableFileFilter(filter);
		int returnVal = fc.showSaveDialog(fereastra.getReportGeneratorFrame()
				.getFrame());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			if (file.getName().substring(file.getName().length() - 4)
					.equals(".xls")) {
				path = file.getPath();
			} else
				path = file.getPath() + ".xls";
			File file1 = new File(path);

			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("ro", "RO"));
			WritableWorkbook workbook = Workbook.createWorkbook(file1, ws);
			WritableSheet s = workbook.createSheet("Sheet1", 0);

			WritableCellFormat cf1obj = new WritableCellFormat(
					NumberFormats.INTEGER);

			for (Angajat angajat : angajati.getSelectedSchool()
					.getListaAngajati())
				if (angajat.getBancaAngajat().equals("Raffeisen Bank"))
					if (angajat.getSuma() != 0) {
						Number c1 = new Number(0, i, Integer.parseInt(angajat
								.getContAngajat()), cf1obj);
						Label c2 = new Label(1, i, angajat.getNumeAngajat());
						Number c3 = new Number(2, i, angajat.getSuma() * 100,
								cf1obj);

						s.addCell(c1);
						s.addCell(c2);
						s.addCell(c3);
						i++;
					}
			workbook.write();
			workbook.close();
		}
	}

	public void setRaifeissenPrintFile(AngajatiData angajati) throws Exception {
		String zona;
		JFileChooser fc = new JFileChooser();
		Integer totalSume = 0;
		File file;
		String path = "";
		String tempString = "Agent economic: ";
		int i = 0;
		FileNameExtensionFilter filter = new FileNameExtensionFilter(".xls",
				"All files");
		fc.addChoosableFileFilter(filter);
		fc.setName("Print File");
		int returnVal = fc.showSaveDialog(fereastra.getReportGeneratorFrame()
				.getFrame());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			if (file.getName().substring(file.getName().length() - 4)
					.equals(".xls")) {
				path = file.getPath();
			} else
				path = file.getPath() + ".xls";
			File file1 = new File(path);

			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("ro", "RO"));
			WritableWorkbook workbook = Workbook.createWorkbook(file1, ws);
			WritableSheet s = workbook.createSheet("Sheet1", 0);

			WritableCellFormat cf1obj = new WritableCellFormat(
					NumberFormats.INTEGER);
			// cf1obj.setBorder(jxl.format.Border.ALL,
			// jxl.format.BorderLineStyle.THICK);
			cf1obj.setAlignment(Alignment.CENTRE);

			s.mergeCells(0, 2, 8, 2);
			tempString += angajati.getSelectedSchool().getNumeScoala();
			Label cell = new Label(0, 2, tempString, cf1obj);
			s.addCell(cell);

			s.mergeCells(0, 3, 8, 3);
			tempString = "Strada "
					+ angajati.getSelectedSchool().getSchoolStreet() + ", nr. "
					+ angajati.getSelectedSchool().getSchoolNumber() + ", "
					+ angajati.getSelectedSchool().getSchoolTown();
			Label secondCell = new Label(0, 3, tempString, cf1obj);
			s.addCell(secondCell);

			s.mergeCells(0, 4, 8, 4);
			tempString = "Stat virament drepturi banesti Raiffeisen Bank";
			Label cell1 = new Label(0, 4, tempString, cf1obj);
			s.addCell(cell1);

			s.mergeCells(0, 5, 8, 5);
			tempString = getMonth(fereastra.getReportGeneratorFrame().getDate()
					.getText().substring(3, 5))
					+ " "
					+ fereastra.getReportGeneratorFrame().getDate().getText()
							.substring(6);

			Label cell2 = new Label(0, 5, tempString,cf1obj);
			s.addCell(cell2);

		/*	cf1obj.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			*/
			WritableCellFormat center = new WritableCellFormat(
					NumberFormats.INTEGER);
			center.setAlignment(Alignment.CENTRE);
			center.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			
			WritableCellFormat left = new WritableCellFormat(
					NumberFormats.INTEGER);
			left.setAlignment(Alignment.LEFT);
			left.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			
			WritableCellFormat right = new WritableCellFormat(
					NumberFormats.INTEGER);
			right.setAlignment(Alignment.RIGHT);
			right.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			
			Label nr = new Label(0, 9, "Nr. crt.",center);
			s.mergeCells(1, 9, 3, 9);
			Label nume = new Label(1, 9, "Nume",center);
			
			s.mergeCells(4, 9, 6, 9);
			Label cont = new Label(4, 9, "Cont IBAN",center);
			
			s.mergeCells(7, 9, 8, 9);
			Label suma = new Label(7, 9, "Drept salarial",center);
			s.addCell(nr);
			s.addCell(nume);
			s.addCell(cont);
			s.addCell(suma);

			for (Angajat angajat : angajati.getSelectedSchool()
					.getListaAngajati())
				if (angajat.getBancaAngajat().equals("Raffeisen Bank"))
					if (angajat.getSuma() != 0) {
						Number c0 = new Number(0, i + 10, i + 1, center);
						s.mergeCells(1, i+10, 3, i+10);
						Label c1 = new Label(1, i + 10,
								angajat.getNumeAngajat(),left);
						s.mergeCells(4, i+10, 6, i+10);
						Number c2 = new Number(4, i + 10,
								Integer.parseInt(angajat.getContAngajat()),
								center);
						s.mergeCells(7, i+10, 8, i+10);
						Number c3 = new Number(7,i+10, angajat.getSuma(), right);
					//	s.mergeCells(20, 3, 25, 5);

						s.addCell(c0);
						s.addCell(c1);
						s.addCell(c2);
						s.addCell(c3);
						i++;
						totalSume += angajat.getSuma();
					}
		//	zona = "A10" + ":A" + (i + 10);
		//	System.out.println(zona);

			s.mergeCells(4, i+10, 6, i+10);
			Label total = new Label(4, i + 10, "Total:",center);
			
			s.mergeCells(7, i+10, 8, i+10);
			Number totalSuma = new Number(7, i + 10, totalSume, right);
			s.addCell(total);
			s.addCell(totalSuma);
			s.addCell(new Label(1, i + 13, "Director,"));
			s.addCell(new Label(6, i + 13, "Contabil,"));
			s.addCell(new Label(1, i + 14, "prof."+angajati.getSelectedSchool().getSchoolPrincipal()));
			s.addCell(new Label(6, i + 14, angajati.getSelectedSchool().getSchoolAccountant()));
			workbook.write();
			workbook.close();
		}

	}

	private String getMonth(String substring) {
		// TODO Auto-generated method stub
		String[] months = new String[12];
		int month = Integer.parseInt(substring);
		months[0] = "Ianuarie";
		months[1] = "Februarie";
		months[2] = "Martie";
		months[3] = "Aprilie";
		months[4] = "Mai";
		months[5] = "Iunie";
		months[6] = "Iulie";
		months[7] = "August";
		months[8] = "Septembrie";
		months[9] = "Octombrie";
		months[10] = "Noiembrie";
		months[11] = "Decembrie";
		return months[month - 1];

	}

	public void setTransilvaniaExportFile(AngajatiData angajati)
			throws Exception {
	
			
			JFileChooser fc = new JFileChooser();
			Integer totalSume = 0;
			File file;
			String path = "";
			String tempString = "Agent economic: ";
			int i = 0;
			FileNameExtensionFilter filter = new FileNameExtensionFilter(".xls",
					"All files");
			fc.addChoosableFileFilter(filter);
			fc.setName("Print File");
			int returnVal = fc.showSaveDialog(fereastra.getReportGeneratorFrame()
					.getFrame());
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = fc.getSelectedFile();
				if (file.getName().substring(file.getName().length() - 4)
						.equals(".xls")) {
					path = file.getPath();
				} else
					path = file.getPath() + ".xls";
				File file1 = new File(path);

				WorkbookSettings ws = new WorkbookSettings();
				ws.setLocale(new Locale("ro", "RO"));
				WritableWorkbook workbook = Workbook.createWorkbook(file1, ws);
				WritableSheet s = workbook.createSheet("Sheet1", 0);

				WritableCellFormat cf1obj = new WritableCellFormat(
						NumberFormats.INTEGER);
				// cf1obj.setBorder(jxl.format.Border.ALL,
				// jxl.format.BorderLineStyle.THICK);
				cf1obj.setAlignment(Alignment.CENTRE);

				s.mergeCells(0, 2, 8, 2);
				tempString += angajati.getSelectedSchool().getNumeScoala();
				Label cell = new Label(0, 2, tempString, cf1obj);
				s.addCell(cell);

				s.mergeCells(0, 3, 8, 3);
				tempString = "Strada "
						+ angajati.getSelectedSchool().getSchoolStreet() + ", nr. "
						+ angajati.getSelectedSchool().getSchoolNumber() + ", "
						+ angajati.getSelectedSchool().getSchoolTown();
				Label secondCell = new Label(0, 3, tempString, cf1obj);
				s.addCell(secondCell);

				s.mergeCells(0, 4, 8, 4);
				tempString = "Stat virament drepturi banesti la Banca Transilvania";
				Label cell1 = new Label(0, 4, tempString, cf1obj);
				s.addCell(cell1);

				s.mergeCells(0, 5, 8, 5);
				tempString = getMonth(fereastra.getReportGeneratorFrame().getDate()
						.getText().substring(3, 5))
						+ " "
						+ fereastra.getReportGeneratorFrame().getDate().getText()
								.substring(6);

				Label cell2 = new Label(0, 5, tempString,cf1obj);
				s.addCell(cell2);

			/*	cf1obj.setBorder(jxl.format.Border.ALL,
						jxl.format.BorderLineStyle.THIN);
				*/
				WritableCellFormat center = new WritableCellFormat(
						NumberFormats.INTEGER);
				center.setAlignment(Alignment.CENTRE);
				center.setBorder(jxl.format.Border.ALL,
						jxl.format.BorderLineStyle.THIN);
				
				WritableCellFormat left = new WritableCellFormat(
						NumberFormats.INTEGER);
				left.setAlignment(Alignment.LEFT);
				left.setBorder(jxl.format.Border.ALL,
						jxl.format.BorderLineStyle.THIN);
				
				WritableCellFormat right = new WritableCellFormat(
						NumberFormats.INTEGER);
				right.setAlignment(Alignment.RIGHT);
				right.setBorder(jxl.format.Border.ALL,
						jxl.format.BorderLineStyle.THIN);
				
				Label nr = new Label(0, 9, "Nr. crt.",center);
				s.mergeCells(1, 9, 3, 9);
				Label nume = new Label(1, 9, "Nume",center);
				
				s.mergeCells(4, 9, 6, 9);
				Label cont = new Label(4, 9, "Cont IBAN",center);
				
				s.mergeCells(7, 9, 8, 9);
				Label suma = new Label(7, 9, "Drept salarial",center);
				s.addCell(nr);
				s.addCell(nume);
				s.addCell(cont);
				s.addCell(suma);

				for (Angajat angajat : angajati.getSelectedSchool()
						.getListaAngajati())
					if (angajat.getBancaAngajat().equals("Banca Transilvania"))
						if (angajat.getSuma() != 0) {
							Number c0 = new Number(0, i + 10, i + 1, center);
							s.mergeCells(1, i+10, 3, i+10);
							Label c1 = new Label(1, i + 10,
									angajat.getNumeAngajat(),left);
							s.mergeCells(4, i+10, 6, i+10);
							Label c2 = new Label(4, i + 10,angajat.getContAngajat(),center);
							s.mergeCells(7, i+10, 8, i+10);
							Number c3 = new Number(7,i+10, angajat.getSuma(), right);
						//	s.mergeCells(20, 3, 25, 5);

							s.addCell(c0);
							s.addCell(c1);
							s.addCell(c2);
							s.addCell(c3);
							i++;
							totalSume += angajat.getSuma();
						}
			//	zona = "A10" + ":A" + (i + 10);
			//	System.out.println(zona);

				s.mergeCells(4, i+10, 6, i+10);
				Label total = new Label(4, i + 10, "Total:",center);
				
				s.mergeCells(7, i+10, 8, i+10);
				Number totalSuma = new Number(7, i + 10, totalSume, right);
				s.addCell(total);
				s.addCell(totalSuma);
				s.addCell(new Label(1, i + 13, "Director,"));
				s.addCell(new Label(6, i + 13, "Contabil,"));
				s.addCell(new Label(1, i + 14, "prof."+angajati.getSelectedSchool().getSchoolPrincipal()));
				s.addCell(new Label(6, i + 14, angajati.getSelectedSchool().getSchoolAccountant()));
				workbook.write();
				workbook.close();
			}

		}
}
